package com.cyl.manager.oms.service;

import cn.hutool.core.collection.CollectionUtil;
import com.cyl.h5.domain.entity.PlaceOrder;
import com.cyl.manager.oms.domain.form.ManagerOrderInfoQueryForm;
import com.cyl.manager.oms.domain.form.ManagerPlaceOrderDTO;
import com.cyl.manager.oms.domain.vo.*;
import com.cyl.manager.oms.mapper.*;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 订单表Service业务层处理
 *
 * @author zcc
 */
@Service
public class OrderInfoService {

    @Autowired
    private PlaceOrderMapper placeholderMapper;

    public Page<ManagerPlaceOrderVO> selectList(ManagerOrderInfoQueryForm query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        List<ManagerPlaceOrderVO> managerOrderVOList = placeholderMapper.selectManagerOrderPage(query);
        if (CollectionUtil.isEmpty(managerOrderVOList)) {
            return new PageImpl<>(managerOrderVOList, page, 0);
        }
        long total = ((com.github.pagehelper.Page) managerOrderVOList).getTotal();

        return new PageImpl<>(managerOrderVOList, page, total);
    }

    /**
     * 处理中
     *
     * @param id
     * @return
     */
    public Boolean handle(Long id) {
        PlaceOrder placeOrder = placeholderMapper.selectById(id);
        placeOrder.setMarkStatus(2);
        placeOrder.setUpdateTime(LocalDateTime.now());
        placeholderMapper.updateById(placeOrder);
        return true;
    }

    /**
     * 处理中
     *
     * @param id
     * @return
     */
    public Boolean send(Long id) {
        PlaceOrder placeOrder = placeholderMapper.selectById(id);
        placeOrder.setMarkStatus(3);
        placeOrder.setUpdateTime(LocalDateTime.now());
        placeholderMapper.updateById(placeOrder);
        return true;
    }
    /**
     * 处理中
     *
     * @param id
     * @return
     */
    public Boolean payment(Long id) {
        PlaceOrder placeOrder = placeholderMapper.selectById(id);
        placeOrder.setMarkStatus(4);
        placeOrder.setUpdateTime(LocalDateTime.now());
        placeholderMapper.updateById(placeOrder);
        return true;
    }

    public Boolean updata(@Valid ManagerPlaceOrderDTO managerPlaceOrder) {
        PlaceOrder placeOrder = placeholderMapper.selectById(managerPlaceOrder.getId());
        placeOrder.setProductName(managerPlaceOrder.getProductName());
        placeOrder.setProductCount(managerPlaceOrder.getProductCount());
        placeOrder.setContactUserName(managerPlaceOrder.getContactUserName());
        placeOrder.setContactPhone(managerPlaceOrder.getContactPhone());
        placeOrder.setCompanyName(managerPlaceOrder.getCompanyName());
        placeOrder.setShippingAddress(managerPlaceOrder.getShippingAddress());
        placeOrder.setUpdateTime(LocalDateTime.now());
        placeholderMapper.updateById(placeOrder);

        return true;
    }
}
